package com.atguigu.tingshu.search.mq;
import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.search.service.impl.SearchServiceImpl;
import com.atguigu.tingshu.vo.album.AlbumStatVoMsg;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SearchReceiver {
    @Autowired
    SearchService searchService;


    @KafkaListener(topics = KafkaConstant.QUEUE_ALBUM_UPPER)
    public void upperAlbum(ConsumerRecord<String, String> record){
        if(StringUtils.isEmpty(record.value())){
            searchService.upperAlbum(Long.parseLong(record.value()));
            log.info("album { "+record.value()+" } 专辑上架成功");
        }
    }

    @KafkaListener(topics = KafkaConstant.QUEUE_ALBUM_LOWER)
    public void lowerAlbum(ConsumerRecord<String,String> record){
        if(StringUtils.isEmpty(record.value())){
            searchService.lowerAlbum(Long.parseLong(record.value()));
            log.info("album { "+record.value()+" } 专辑下架成功");
        }
    }

    @KafkaListener(topics = KafkaConstant.QUEUE_ALBUM_ES_STAT_UPDATE_SYNC )
    public void updateAlbumStat(ConsumerRecord<String,String> record){
        String value = record.value();
        if(!StringUtils.isEmpty(record.value())){
            AlbumStatVoMsg albumStatVoMsg = JSON.parseObject(value, AlbumStatVoMsg.class);
            searchService.updateAlbumStat(albumStatVoMsg);
            log.info("来自于canal的专辑统计信息更新完毕。。。。{}",JSON.toJSONString(AlbumStatVoMsg.class));
        }
    }
}
